<?php
// +----------------------------------------------------------------------
// * Created by PhpStorm.
// +----------------------------------------------------------------------
// * Author: D H
// +----------------------------------------------------------------------
// * Date: 2017/9/4
// +----------------------------------------------------------------------
// * Time: 12:24
// +----------------------------------------------------------------------

    // 应用公共文件
    use OSS\OssClient;
    use OSS\Core\OssException;
    use think\Db;

    //curl
    function CURLSend($url, $method = 'get', $data = '')
    {
        $ch = curl_init();   //初始化
        $headers = array('Accept-Charset:utf-8');
        //设置url相应的选项
        curl_setopt($ch, CURLOPT_URL, $url);   //需要获取的 URL 地址，也可以在curl_init() 初始化会话的时候
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); //提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);      //全部数据使用HTTP协议中的 "POST" 操作来发送
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     //TRUE 将curl_exec()获取的信息以字符串返回，而不是直接输出
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    //FALSE 禁止 cURL 验证对等证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);    //设置为 1 是检查服务器SSL证书中是否存在一个公用名
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);       //设置HTTP头字段的数组
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0(compatible;MSIE5.01;Windows NT 5.0)'); //头的字符串
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json_data = curl_exec($ch);    //执行会话
        $array = json_decode($json_data, true);
        return $array;
    }

    //阿里云 使用OSS域名新建OssClient
    function ossClient(){
        require_once ROOT_PATH  . 'aliyun/autoload.php';
        $config = config('ALIOSS_CONFIG');
        $accessKeyId = $config['KEY_ID'];
        $accessKeySecret = $config['KEY_SECRET'];
        $endpoint = $config['END_POINT'];
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            return $ossClient;
        } catch (OssException $e) {
            print $e->getMessage();
        }
    }

    /**
     * 上传指定的本地文件内容
     *
     * @param OssClient $ossClient OSSClient实例
     * @param string $bucket 存储空间名称
     * @return null
     */
    function uploadFile($ossClient, $bucket,$object,$filePath)
    {
        //$object = "oss-php-sdk-test/upload-test-object-name.txt";
        //$filePath = __FILE__;
        try{
            $ossClient->uploadFile($bucket, $object, $filePath);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        //print(__FUNCTION__ . ": OK" . "\n");
        return 1;
    }

    /**
     * 删除object
     *
     * @param OssClient $ossClient OSSClient实例
     * @param string $bucket bucket名字
     * @return null
     */
    function deleteObject($ossClient, $bucket,$object)
    {
        //$object = "oss-php-sdk-test/upload-test-object-name.txt";
        try{
            $ossClient->deleteObject($bucket, $object);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        // print(__FUNCTION__ . ": OK" . "\n");
        return 1;
    }

    //得到导航栏面包屑
    function mbx($productID)
    {
        $cat = Db::name('product')->where('id',$productID)->find();
        $ar = [];
        $ar[] = $cat['name'];
        while ($cat['catalogID'] > 0) {
            $db = Db::name('cat')->where('id',$cat['catalogID'])->find();
            $ar[] = $db['name'];
            $cat['catalogID'] = $db['parentID'];
        }
        $ar = array_reverse($ar);
        $arr = '';
        foreach ($ar as $key => $val) {
            $arr .= '>'.$val;
        }
        $mbx = '主页'.$arr;
        return $mbx;
    }
